//
//  CarInfoFullCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 9/3/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit

final class CarInfoFullCell: UITableViewCell {

    // MARK: - Outlets

    private lazy var separatorView = CellSeparator()
    
    private lazy var bannerImg: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.image = UIImage(named: "sedan")
        return imgView
    }()
    
    private lazy var itemsStack = ItemsStack()
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CarInfoFullCell: ConfigurableCell {
    
    func configure(data object: MainViewModel.CarInfoItems?, index: Int) {
        var _data = object!
        itemsStack.setUpItems(data: _data.items)
    }

    func returnData() -> MainViewModel.CarInfoItems? {
        return nil
    }
    
}


extension CarInfoFullCell {
    
    private func addViews () {
        contentView.addSubview(separatorView)
        contentView.addSubview(bannerImg)
        contentView.addSubview(itemsStack)
        
        bannerImg.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(30)
            make.width.equalTo(155)
            make.height.equalTo(55)
            make.centerX.equalToSuperview()
        }
        
        itemsStack.snp.makeConstraints { (make) in
            make.top.equalTo(bannerImg.snp.bottom).offset(25)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(separatorView.snp.top).offset(-16)
        }
        
        separatorView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(20)
        }
    }
}



final class CarGeneralInfoCell: UITableViewCell {

    // MARK: - Outlets

    private lazy var separatorView = CellSeparator()

    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .bold)
        lbl.textAlignment = .left
        lbl.sizeToFit()
        lbl.textColor = .black
        lbl.text = "Общая информация"
        return lbl
    }()
    
    private lazy var itemsStack = ItemsStack()
    
    // MARK: - Init
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CarGeneralInfoCell: ConfigurableCell {
    
    func configure(data object: CarGeneralData?, index: Int) {
        
        let items: [ItemsStack.KeyValueItem] = [
            ItemsStack.KeyValueItem(value: object?.origin_country ?? "Неизвестно", hint: "Страна марки"),
            ItemsStack.KeyValueItem(value: object?.vehicle_category ?? "Неизвестно", hint: "Класс автомобиля"),
            ItemsStack.KeyValueItem(value: object?.seating_count ?? "Неизвестно", hint: "Количество мест"),
            ItemsStack.KeyValueItem(value: object?.color ?? "Неизвестно", hint: "Цвет"),
            ItemsStack.KeyValueItem(value: object?.engine_volume ?? "Неизвестно", hint: "Объем двигателя"),
            ItemsStack.KeyValueItem(value: object?.engine_power_kwt ?? "Неизвестно", hint: "Мощность мотора")
        ]
        
        itemsStack.setUpItems(data: items)
    }

    func returnData() -> CarGeneralData? {
        return nil
    }
    
}


extension CarGeneralInfoCell {
    
    private func addViews () {
        contentView.addSubview(separatorView)
        contentView.addSubview(itemsStack)
        contentView.addSubview(titleLbl)
        
        titleLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(22)
            make.height.equalTo(20)
        }
        
        itemsStack.snp.makeConstraints { (make) in
            make.top.equalTo(titleLbl.snp.bottom).offset(18)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(separatorView.snp.top).offset(-16)
        }
        
        separatorView.snp.makeConstraints { (make) in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(20)
        }
    }
}


